package com.baseproject.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.ViewGroup;

import com.baseproject.mvp.MvpFragment;


public abstract class BaseMultiItemDelegateAdapter extends RecyclerView.Adapter<ViewHolder> implements ItemViewDelegateManager.DelegateManagerCallBack {

    protected Context context;
    protected MvpFragment mFragment;
    protected ItemViewDelegateManager mItemViewDelegateManager;

    protected final static int GET = 1;
    protected final static int POST = 2;
    /**
     * 设置是否显示EmptyView
     */
    protected boolean showEmptyView = false;
    /**
     * 标识是否显示EmptyView
     */
    protected boolean isShowEmptyView = false;
    /**
     * 全部加载完毕是否显示底部View
     */
    protected boolean isShowEndView = false;
    /**
     * 是否添加了显示底部View的数据
     */
    protected boolean isAddShowEndViewData = false;

    public BaseMultiItemDelegateAdapter() {

    }

    public BaseMultiItemDelegateAdapter(Context context) {
        this(null, context);
    }

    public BaseMultiItemDelegateAdapter(MvpFragment fragment, Context context) {
        this.mFragment = fragment;
        this.context = context;
        mItemViewDelegateManager = new ItemViewDelegateManager(this);
        addItemViewDelegates();
    }

    protected abstract void addItemViewDelegates();

    public Fragment getFragment() {
        return mFragment;
    }

    public void setFragment(MvpFragment fragment) {
        this.mFragment = fragment;
    }

    public abstract boolean onKeyDown(int keyCode, KeyEvent event);

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder holder = mItemViewDelegateManager.createViewHolder(mFragment.getActivity(), parent, viewType);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        mItemViewDelegateManager.convert(holder, position);
    }

    @Override
    public int getItemCount() {
        return mItemViewDelegateManager.getItemViewCount();
    }

    /**
     * 决定元素的布局使用哪种类型
     *
     * @param position 数据源的下标
     * @return 一个int型标志，传递给onCreateViewHolder的第二个参数
     */
    @Override
    public int getItemViewType(int position) {
        if (!useItemViewDelegateManager()) return super.getItemViewType(position);
        return mItemViewDelegateManager.getItemViewType(position);
    }

    protected boolean useItemViewDelegateManager() {
        return mItemViewDelegateManager.getItemViewDelegateTypeCount() > 0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public <T> void updateData(int viewType, T t) {
        mItemViewDelegateManager.updateData(viewType, t);
    }

    ///////////////////////////////////////////////

    public void setShowEmptyView(boolean showEmptyView) {
        this.showEmptyView = showEmptyView;
    }

    public void setIsShowEmptyView(boolean show) {
        if (!showEmptyView) return;
        isShowEmptyView = show;
        if (isShowEmptyView) {
        }
    }
}
